package com.ling.luo.demo;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.ling.luo.config.ali.Alipay;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/pay")
public class PayController {

    public String refund() {
        String refund = Alipay.refund("B004", "2020122122001403330501103932", "12", "我要退款");
        return null;
    }

    /**
     * 关闭订单请求
     * @param sn
     * @return
     */
    @GetMapping("/close_pay")
    @ResponseBody
    public String closePay(String sn) {
        sn = "B004";
         String closePay = Alipay.closePay(sn);
         System.out.println(new Date().toLocaleString() + "  " + closePay);
        JSONObject parseObject = JSONObject.parseObject(closePay);
        int code = parseObject.getJSONObject("alipay_trade_close_response").getIntValue("code");
        if (code != 10000) {
            throw new RuntimeException(parseObject.getJSONObject("alipay_trade_close_response").getString("sub_msg"));
        }
        return "success";
    }
    @GetMapping("/alipay")
    public String alipay(Model model) {
        String html = Alipay.AliPay("B004", new BigDecimal(342.5), "第2单", "这是一个第一个阿里支付宝沙箱支付");
        model.addAttribute("html", html);
        return "show1";
    }

    /**
     * 支付宝异步通知接口
     * 由于时ali接口请求，登录时不做拦截，过滤掉该接口，不然请求无法进入
     * @param request
     * @return
     */
    @GetMapping("/alipayNotify")
    @ResponseBody
    public String alipayNotify(HttpServletRequest request) {
        // 校验异步通知是否合法
        if (!Alipay.isValid(request)) {
            return "fail";
        }
        // 订单号
        String sn = request.getParameter("out_trade_no");
        // 支付宝交易号
        String paySn = request.getParameter("trade_no");
        // 交易金额
        String totalAmount = request.getParameter("total_amount");
        // 支付状态
        String status = request.getParameter("trade_status");
        if (StringUtils.isEmpty(sn)) {
            System.err.println(new Date().toLocaleString() + " 订单号为空");
            return "fail";
        }if (StringUtils.isEmpty(paySn)) {
            System.err.println(new Date().toLocaleString() + " 支付宝交易号为空");
            return "fail";
        }if (StringUtils.isEmpty(totalAmount)) {
            System.err.println(new Date().toLocaleString() + " 交易金额为空");
            return "fail";
        }if (StringUtils.isEmpty(status)) {
            System.err.println(new Date().toLocaleString() + " 支付状态为空");
            return "fail";
        }
        // 到这里表示参数都是合法的
        /**
         * 1. 查找订单信息
         * 2. 判断金额是否一致
         */
        /*PayLog payLog = payLogService.findBySn(sn);
        if (Objects.isNull(payLog)) {
            System.err.println(new Date().toLocaleString() + " 订单编号不存在");
            return "fail";
        }
        if (payLog.getTotalAmount().compareTo(new BigDecimal(totalAmount))) {
            System.err.println(new Date().toLocaleString() + " 支付金额不同");
            return "fail";
        }*/
        // 表示一切合法
        // 操作数据库
        return "success";
    }
}
